export class Vector {
    size: number;
    data: Float32Array;
    constructor(size: number) {
        this.size = size;
        this.data = new Float32Array(this.size);
    }

    loadFromVector(v: Vector, ...others:number[]) {
        for (let i=0; i<this.size;  i++) {
            if (i < v.size) {
                this.data[i] = v.data[i];
            } else if (i < v.size+others.length) {
                this.data[i] = others[i-v.size];
            }
        }
    }

    loadFromStringArray(datas: string[]) {
        for (let i =0 ; i<this.size && i < datas.length; i++) {
            this.data[i] = Number(datas[i]);
        }
        return this;
    }

    loadFromString(str: string, splitStr: string = ' ') {
        this.loadFromStringArray(str.split(splitStr).filter(s  => !!s));
        return this;
    }

    toString() {
        return  `[${this.data.join(',  ')}]`;
    }


    clone() {
        // @ts-ignore
        const re = new this.constructor() as this;
        re.data.set(this.data);
        return re;
    }

    multiplyVector(vector: Vector): this {
        for (let i = 0; i < this.size && i  < vector.size; i++) {
            this.data[i] *= vector.data[i];
        }
        return this;
    }

    multipliedVector(vector: Vector) {
        const re = this.clone();
        for (let i = 0; i < this.size && i  < vector.size; i++) {
            re.data[i] *= vector.data[i];
        }
        return re;
    }

    multiplyScale(scale: number) {
        for (let i=0; i < this.size; i++) {
            this.data[i] *= scale;
        }
        return this;
    }
    multipliedScale(scale: number) {
        const re = this.clone();
        for (let i=0; i < this.size; i++) {
            re.data[i] *= scale;
        }
        return re;
    }

}